// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
//#include "mk_render_step_reflection.h"
//#include "mk_render_target.h"
//#include "mk_engine.h"
//#include "mk_error.h"
//#include "mk_render_device.h"
//#include "mk_resource_manager.h"
//#include "mk_render_step_view_culling.h"
//#include "mk_texture.h"
//#include "mk_render_frame.h"
//#include "mk_entity.h"
//#include "mk_camera.h"
//#include "mk_scene_node.h"
//#include "mk_culling_processor_default.h"
//
//namespace MK3DE
//{
//	const char* CRenderStepReflection::RenderStepName = "__RenderStepReflection";
//
//	CRenderStepReflection::CRenderStepReflection(CRenderFrame* renderFrame)
//		: CRenderStep(renderFrame, CRenderStepReflection::RenderStepName)
//	{
//		_rtReflection = NULL;
//		_reflectionTextureSize = 512;
//	}
//
//	CRenderStepReflection::~CRenderStepReflection()
//	{
//		CRenderDevice* rd = GetEngine()->GetRenderDevice();
//		rd->DestroyRenderTarget(_rtReflection);
//	}
//
//
//	BOOL CRenderStepReflection::__CheckReflectionRenderTarget()
//	{
//		if (_rtReflection)
//			return TRUE;
//
//		CEngine* engine = GetEngine();
//		CRenderDevice* renderDevice = engine->GetRenderDevice();
//		return renderDevice->CreateRenderTarget(_reflectionTextureSize, _reflectionTextureSize, PF_A8R8G8B8, FALSE, _rtReflection);
//	}
//
//	void CRenderStepReflection::__MakeReflectionPlane(float height, D3DXPLANE& planeReflection)
//	{
//		D3DXPlaneFromPointNormal(&planeReflection, &D3DXVECTOR3(0, height, 0), &D3DXVECTOR3(0, 1, 0));
//
//		D3DXMATRIX matReflection;
//		D3DXMatrixReflect(&matReflection, &planeReflection);
//
//		CCamera* pCamera = GetEngine()->GetCurrentCamera();
//
//		D3DXVECTOR3 posSave = pCamera->GetPosition();
//		D3DXVECTOR3 lookDirSave = pCamera->GetLookDirection();
//		D3DXVECTOR3 upDirSave = pCamera->GetUpDirection();
//
//		D3DXVECTOR3 pos, lookDir, upDir;
//		D3DXVec3TransformCoord(&pos, &posSave, &matReflection);
//		D3DXVec3TransformNormal(&lookDir, &lookDirSave, &matReflection);
//		D3DXVec3TransformNormal(&upDir, &upDirSave, &matReflection);
//
//		pCamera->SetPosition(pos);
//		pCamera->SetLookDirection(lookDir);
//		pCamera->SetUpDirection(upDir);
//		pCamera->AddCustomPlane(planeReflection);
//		pCamera->Update(0);
//
//		D3DXMATRIX matViewIT;
//		D3DXMatrixInverse(&matViewIT, NULL, &pCamera->GetViewMatrix());
//		D3DXMatrixTranspose(&matViewIT, &matViewIT);
//
//		D3DXMATRIX matProjIT;
//		D3DXMatrixInverse(&matProjIT, NULL, &pCamera->GetProjMatrix());
//		D3DXMatrixTranspose(&matProjIT, &matProjIT);
//
//		D3DXPlaneTransform(&planeReflection, &planeReflection, &(matViewIT * matProjIT));
//	}
//
//
//	void CRenderStepReflection::Render()
//	{
//		CRenderStepViewCulling* rsCulling = (CRenderStepViewCulling*)_renderFrame->GetRenderStepByName(CRenderStepViewCulling::RenderStepName);
//		CCullingProcessor::CullingResult cullingResult = rsCulling->GetCullingResult();
//		std::vector<CEntity*>& vecEntityWater = cullingResult.vecEntity[ERT_WATER];
//		if (vecEntityWater.size() == 0)
//			return;
//
//		float height = rsCulling->GetVisibleWaterHeight();
//
//		CCullingProcessorDefault cullingProcessor;
//		CCullingProcessor::CullingResult result;
//		cullingProcessor.Culling(_scene, pCamera, result);
//
//		D3DXPLANE planeReflection;
//		__MakeReflectionPlane(height, planeReflection);
//
//		CEngine* engine = GetEngine();
//		CRenderDevice* renderDevice = engine->GetRenderDevice();
//
//		if (!__CheckReflectionRenderTarget())
//			return;
//
//		renderDevice->SetRenderTarget(_rtReflection);
//		renderDevice->Clear(TRUE, TRUE, TRUE, MK_COLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
//		renderDevice->SetClipPlane(0, planeReflection, TRUE);
//
//		for (int i = 0; i < (int)ERT_MAX; ++i)
//		{
//			if (ERT_WATER == (ENTITY_RENDER_TYPE)i)
//				continue;
//
//			std::vector<CEntity*>& vecEntity = cullingResult.vecEntity[i];
//			std::vector<CEntity*>::iterator it = vecEntity.begin();
//			while (it != vecEntity.end())
//			{
//				CEntity* entity = *it;
//				entity->Render();
//
//				++it;
//			}
//		}		
//
//		renderDevice->SetClipPlane(0, D3DXPLANE(0, 0, 0, 0), FALSE);
//	}
//
//	void CRenderStepReflection::SetReflectionTextureSize(DWORD size)
//	{
//		CRenderDevice* rd = GetEngine()->GetRenderDevice();
//		if (_rtReflection && _reflectionTextureSize != size)
//			rd->DestroyRenderTarget(_rtReflection);
//
//		_reflectionTextureSize = size;
//	}
//}



